package com.example.demo.controller;

import com.example.demo.main.Admin;
import com.example.demo.service.AdminService;
import com.example.demo.service.UseSQLOfOrder;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/Admin")
public class AdminDataController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private UseSQLOfOrder useSQLOfOrder;
    @Autowired
    private UserService userService;

    @RequestMapping("createAdmin/data")
    public String createAdminData(Admin admin) {
        if (admin != null && admin.getAdminName() != null) {
            if (adminService.isHPower(admin.getAdminName())) {
                if (adminService.insertByClass(admin) > 0)
                    return "yes";
            } else return "您无此权限！！";
        }
        return "未知错误！！";
    }

    @RequestMapping("/checkPwd/Data")
    public String checkPwdData(Admin admin, HttpServletRequest request) {
        if (admin != null && admin.getAdminName() != null) {
            if (adminService.checkPwd(admin)) {
                String power = adminService.getPower(admin.getAdminName());
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:SSS");
                Date newDate = new Date();
                request.getSession().setAttribute("adminName", admin.getAdminName());
                request.getSession().setAttribute("adminTime", formatter.format(newDate));
                request.getSession().setAttribute("power", power);
                return "yes";
            }
        }
        return "no";
    }

    @RequestMapping("/exit/data")
    public String toExit(HttpServletRequest request) {
        request.getSession().removeAttribute("adminName");
        request.getSession().removeAttribute("adminTime");
        request.getSession().removeAttribute("power");
        return "yes";
    }

    @RequestMapping("/deleteOrder/data")
    public String deleteOrder(String orderId) {
        if (orderId != null && !orderId.equals("")) {
            if (useSQLOfOrder.deleteByID(orderId) > 0)
                return "yes";
        }
        return "no";
    }

    @RequestMapping("updateAdmin/data")
    public String updateAdmin(String val, String str, String name) {//val值,str字段名
        if (val != null && str != null && name != null
                && !val.equals("")) {
            if (adminService.updateByName(val, str, name))
                return "yes";
        }
        return "no";
    }

    @RequestMapping("/updateUser/data")
    public String updateUser(String val, String str, String name) {
        if (val != null && str != null && name != null
                && !val.equals("")) {
            if (userService.updateByName(val, str, name))
                return "yes";
        }
        return "no";
    }
}
